// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

cc.Class({
    extends: cc.Component,
    properties: {
        skinNode:cc.Node,
    },
    onLoad () {
        //微信平台
        if(cc.sys.platform == cc.sys.WECHAT_GAME){

            var width = wx.getSystemInfoSync().windowWidth;
            //创建开始按钮
            this.wxStartButton = wx.createUserInfoButton({     
                type: 'image',
                image:cc.url.raw('resources/startGame.png'),         
                style: {         
                    left: wx.getSystemInfoSync().windowWidth*0.1,        
                    top: wx.getSystemInfoSync().windowHeight*0.45,         
                    width: wx.getSystemInfoSync().windowWidth*0.8,         
                    height: wx.getSystemInfoSync().windowWidth*0.16
                } 
            });
            //显示开始按钮
            this.wxStartButton.show();

            //点击开始按钮
            var self = this;
            this.wxStartButton.onTap(function(res){ 
                if(res.rawData){
                    var userInfos  = JSON.parse(res.rawData);
                    console.log("user infos = ",userInfos);
                    var expresionTools = require("expresionTools");
                    expresionTools.setData("user_name",userInfos.nickName);
                    expresionTools.setData("user_icon",userInfos.avatarUrl);
                    wx.login({success:function(e){
                        var url = "https://api.vzhifu.net/wxCodeToken?js_code=" + e.code + "&pid="+'24666';
                        expresionTools.Get(url,function(res){ expresionTools.setData("user_openid",res.openid) });
                        cc.director.loadScene('game');
                        self.wxStartButton.hide();
                    }})
                }
            })
            
        }
    },

    start () {

    },

    funcBtnClicked (event, customEventData) {
        if('share' == customEventData)
        {//分享
            console.log('share');
        }
        else if ('skin' == customEventData)
        {//皮肤界面
            console.log('skin');
            this.skinNode.active = true;
            if(cc.sys.platform == cc.sys.WECHAT_GAME){
                this.wxStartButton.hide();
            }
        }
        else if ('bang' == customEventData)
        {//榜单
            console.log('bang');
        }
        else
        {//更多游戏
            console.log('more game');
        }
    },
    closeTopView(event, customEventData){
        if('skin' == customEventData) { 
            this.skinNode.active = false;
            this.wxStartButton.show();
        }
    },
    // update (dt) {},
});
